﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.Utils;
using HitachiLib.Data;
using HitachiLib.HitachiServiceRef;
using HitachiApp.Model.Projects;

namespace HitachiApp.Model.Manage
{
    public class ProjectBrowserModel : INotifyPropertyChanged
    {
        public ProjectBrowserModel(ManageViewModel parent)
        {
            manage_view_model = parent;
        }

        private ManageViewModel manage_view_model;
        private ObservableCollection<ProjectManageItem> project_list = new ObservableCollection<ProjectManageItem>();
        ProjectManageItem current_selected;

        public ObservableCollection<ProjectManageItem> RootProjects
        {
            get { return project_list; }
        }
        public ProjectManageItem CurrentSelected
        {
            get { return current_selected; }
            set {
                current_selected = value;
                manage_view_model.RefreshCurrent();
            }
        }

        public void Load(Collection<ProjectData> list)
        {
            List<ProjectManageItem> all = new List<ProjectManageItem>();
            foreach (ProjectData pd in list)
            {
                Project p = Project.GetUnique(pd);
                ProjectManageItem pmi = new ProjectManageItem();
                pmi.Data = p;
                all.Add(pmi);
            }
            LinkProjects(all);
        }

        private void LinkProjects(List<ProjectManageItem> project_items)
        {
            Dictionary<int, ProjectManageItem> m = new Dictionary<int, ProjectManageItem>();
            Dictionary<int, ProjectManageItem> added = new Dictionary<int, ProjectManageItem>();
            project_list.Clear();
            foreach (ProjectManageItem pmi in project_items)
            {
                if (!m.ContainsKey(pmi.Data.Id))
                    m.Add(pmi.Data.Id, pmi);
            }
            foreach (ProjectManageItem pmi in project_items)
            {
                AddProjectWithParent(m, added, pmi);
            }
        }

        private void AddProjectWithParent(Dictionary<int, ProjectManageItem> all, Dictionary<int, ProjectManageItem> added, ProjectManageItem pmi)
        {
            if (added.ContainsKey(pmi.Data.Id))
                return;
            if (!all.ContainsKey(pmi.Data.Data.ParentID))
            {
                project_list.Add(pmi);
            }
            else
            {
                ProjectManageItem parent = all[pmi.Data.Data.ParentID];
                pmi.Parent = parent;
                parent.Data.AddChild(pmi.Data);
                if (!parent.Children.Contains(pmi))
                    parent.Children.Add(pmi);
                AddProjectWithParent(all, added, parent);
            }
            added.Add(pmi.Data.Id, pmi);
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
